<template>
  <div class="control-box">
    <p>购买数量</p>
    <div class="handle-box">
      <button type="button" class="btn reduce" @click="handleReduce">-</button>
      <input type="text" class="num" v-model="val" @input="onInput">
      <button type="button" class="btn add" @click="handleAdd">+</button>
    </div>
  </div>
</template>

<script>
const isNum = /^[1-9]\d*$/;
import { store } from "../../api/store";
export default {
  data() {
    return {
      // val: this.$store.state.num
    };
  },
  computed: {
    val: {
      get () {
        return this.$store.state.num
      },
      set (val) {
        this.$store.commit('setNum', val)
      }
    }
  },
  methods: {
    onInput() {
      let num = this.$store.state.num;
      console.log(num)
      if (!isNum.test(num)) {
        this.$toast("请输入正整数");
        this.$store.commit('reset')
      }
    },
    handleReduce() {
      this.$store.commit('decrement')
    },
    handleAdd() {
      this.$store.commit('increment')
    }
  }
};
</script>

<style lang="less" scoped>
.control-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .handle-box {
    display: flex;
    align-items: center;
    .btn {
      background: #fff;
      border: 0;
      outline: none;
      font-size: 5vw;
    }
    .num {
      width: 12vw;
      text-align: center;
      margin: 0 2vw;
      background: #f5f5f5;
      border: 0;
      padding: 0.5vw 0;
    }
  }
}
</style>